<?php

namespace App\Repository\AppUser;

use App\Jobs\GetOrderStatus;
use App\Models\AppUser\TouTiaoUserOrderModel as model;
use App\Repository\BaseRepository;

class TouTiaoUserOrderRepository extends BaseRepository
{
    private $model = null;

    public function __construct()
    {
        $this->model = new model();
        parent::__construct();
    }

    public function index($request)
    {
        $cron['where']['shop_id'] = $request->user->shop_id;
        if (!empty($request['created_start']) && !empty($request['created_end'])) {
            $cron['between']['create_time'][] = $request['created_start'];
            $cron['between']['create_time'][] = $request['created_end'];
        }
        if (!empty($request['number']) && !empty($request['number'])) {
            $cron['where']['name'][] = $request['number'];
        }

        $cron['order']['orderby'] = isset($request['orderby']) ? $request['orderby'] : 'id';
        $cron['order']['orderdir'] = isset($request['orderdir']) ? $request['orderdir'] : 'desc';
        $pageNums = 0;
        $cron['limit']['number'] = isset($request['pagenums']) ? ($request['page_size']) : 10;
        if (isset($request['page']) && !empty(($request['page']))) {
            $pageNums = ($request['page'] - 1) * $cron['limit']['number'];
        }
        $cron['limit']['start'] = $pageNums;
        if (!empty($request->get('params'))) {
            $time = $request->get('params');
            $where['between'] = ['douyin_remark.created_at' => [$time['beginTime'], $time['endTime']]];
        }
        return $this->search($this->model, $cron, 1, 10);
    }

    public function addOrEdit($data)
    {
        //必填 字段
        $mustField = [
            'orderno'
        ];
        $optionalFields = $this->model->getTableField();
        $msgField = [
            'orderno' => '订单号不能为空'
        ];
        $insertData = $this->getRealData($data, $mustField, $optionalFields, $msgField);
        if (!empty($insertData) && is_string($insertData)) {
            //返回错误参数
            return $this->returnMsg(
                2,
                $insertData
            );
        }
        $isTrue = $this->model->where('orderno', $insertData['orderno'])->first();
        if ($isTrue) {
            // $edit = $this->model->where('orderno', $insertData['orderno'])->update($insertData);
            // if ($edit) {
            //   return $this->returnMsg(100,'修改失败');
            // }

            return $this->returnMsg(200, '已存在');
        }
        $insertData['created_at'] = date('Y-m-d H:i:s');
        // 查询是否存在 如果存在 直接流转

        $res = $this->model->insertGetId($insertData);
        if ($res) {
            if (!empty($insertData['status']) && $insertData['status'] != 4 && $insertData['status'] != 5) {
                dispatch(new GetOrderStatus($insertData['orderno'], 'douyinPaySyncOrder', 1))->onQueue('TaskGetOrderStatusDispose'); // 抖音支付同步订单
            }

            return $this->returnMsg(200, '添加成功', ['user_id' => $res]);
        }
        return $this->returnMsg(1, '添加失败');
    }

    //添加
    public function add($data)
    {
        //必填 字段
        $mustField = [
            'shop_id'
        ];
        $optionalFields = $this->model->getTableField();
        $msgField = [
            'shop_id' => '名称不能为空'
        ];
        $insertData = $this->getRealData($data, $mustField, $optionalFields, $msgField);
        if (!empty($insertData) && is_string($insertData)) {
            //返回错误参数
            return $this->returnMsg(
                2,
                $insertData
            );
        }

        $insertData['created_at'] = date('Y-m-d H:i:s');
        // 查询是否存在 如果存在 直接流转

        $res = $this->model->insert($insertData);
        if ($res) {
            return $this->returnMsg(200, '添加成功');
        }
        return $this->returnMsg(1, '添加失败');
    }

    //删除
    public function del($where)
    {
        $flag = true;
        $msg = '未知错误';
        $code = 1;
        if (empty($where)) {
            $flag = false;
            $msg = '删除条件不能为空';
            $code = 2;
        }
        if ($flag) {
            $res = $this->model->where($where)->get();
            if (!$res) {
                $flag = false;
                $msg = '未查询到数据';
                $code = 3;
            }
        }
        if ($flag) {
            $del = $this->model->where($where)->delete();
            if ($del) {
                $msg = '删除成功';
                $code = 200;
            }
        }
        return $this->returnMsg($code, $msg);
    }

    //修改
    public function edit($where, $data, $isMust = false)
    {
        if (empty($where)) {
            return $this->returnMsg(1, '条件不能为空');
        }
        if (empty($data)) {
            return $this->returnMsg(2, '修改参数不能为空');
        }
        //过滤字段
        $optionalFields = $this->model->getTableField();
        $field = $this->getRealData($data, [], $optionalFields, []);
        $isTrue = $this->model->where($where)->first();
        if (!$isTrue) {
            return $this->returnMsg(100, '未查询到修改的数据');
        }
        if ($isTrue->status == 2 && $field['status'] == 2) {
            return $this->returnMsg(200, '已修改');
        }
        //先查询是否存在
        // 判断用户的金额是否为空
        $orderno = $isTrue->orderno;
        if (!empty($isTrue->money) && !empty($isTrue->status) && $isTrue->status == 2) {
            // 调用用户退款
            $field['status'] = 3;
            $field['out_refund_no'] = creatOrderNums('TK');

            dispatch(new GetOrderStatus($orderno, 'douyinRefundSyncOrder', 1))->onQueue('TaskGetOrderStatusDispose'); // 抖音退款
        }
        $res = $this->model->where($where)->update($field);
        if (!empty($field['status']) && $field['status'] != 4 && $field['status'] != 5) {
            dispatch(new GetOrderStatus($orderno, 'douyinPaySyncOrder', 1))->onQueue('TaskGetOrderStatusDispose'); // 抖音支付同步订单
        }

        if ($isMust && $res) {
            return $this->returnMsg(200, '修改成功');
        }
        if (!$isMust) {
            return $this->returnMsg(200, '修改成功');
        }
        return $this->returnMsg(3, '请重新修改');
    }

    //查看  存入缓存
    public function sel($where = [], $page = 1, $limit = 10)
    {
        $res = $this->search($this->model, $where, $page, $limit);
        return $res;
    }

    //查询 单条数据
    public function oneSel($where)
    {
        return $this->model->where($where)->first();
    }

    public function getOne($where, $field = ['*'])
    {
        return $this->model->where($where)->select($field)->orderBy('id', 'desc')->first();
    }

    public function getAll($where, $field = ['*'])
    {
        return $this->model->where($where)->select($field)->get();
    }

    public function getStatusDesc($statsu)
    {
        return $this->model->getStatusDesc($statsu);
    }
}
